<?php
require "IncludeManager.php";

using("Expression_Operand");
using("Expression_Operand_Variable");
using("Expression_Operand_Table");
using("Expression_Operator_Factory");

## Simple example constructing a select query with a where clause
$factory = new Expression_Operator_Factory();

$l1 = new Expression_Operand("SELECT * FROM users");

$l2 = new Expression_Operand("username");
$l2v = new Expression_Operand_Variable("foobar");
$e1 = $factory->construct("equals", array($l2, $l2v));

$l3 = new Expression_Operand("password");
$l3v = new Expression_Operand_Variable("apsw4foobar");
$e2 = $factory->construct("equals", array($l3, $l3v));

$and1 = $factory->construct("and", array($e1, $e2));

$where = $factory->construct("where", array($and1));

$root = $factory->construct("nothing", array($l1, $where)); // this is an unfortunate special case
echo $root . "<br />";

## Same example but with a not stuck in for good measure
$l1 = new Expression_Operand("SELECT * FROM users");

$l2 = new Expression_Operand("username");
$l2v = new Expression_Operand_Variable("foobar");
$e1 = $factory->construct("equals", array($l2, $l2v));

$l3 = new Expression_Operand("password");
$l3v = new Expression_Operand_Variable("apsw4foobar");
$e2 = $factory->construct("equals", array($l3, $l3v));

$not = $factory->construct("not", array($e2));

$and1 = $factory->construct("and", array($e1, $not));

$where = $factory->construct("where", array($and1));

$root = $factory->construct("nothing", array($l1, $where)); // this is an unfortunate special case
echo $root . "<br />";

## Now an insert statement
$table = new Expression_Operand_Table("users");

$l1 = new Expression_Operand("username");
$l1v = new Expression_Operand("pasword");
$sep1 = $factory->construct("separator", array($l1, $l1v));
$gro1 = $factory->construct("group", array($sep1));

$l2 = new Expression_Operand_Variable("johndoe");
$l2v = new Expression_Operand_Variable("foobar");
$sep2 = $factory->construct("separator", array($l2, $l2v));
$gro2 = $factory->construct("group", array($sep2));

$val = $factory->construct("value", array($gro2));

$root = $factory->construct("insert", array($table, $gro1, $val));
echo $root . "<br />";

# Testing update
$table = new Expression_Operand_Table("users");

$l1 = new Expression_Operand("password");
$l1v = new Expression_Operand_Variable("foobar");
$e1 = $factory->construct("equals", array($l1, $l1v));

$l2 = new Expression_Operand("status");
$l2v = new Expression_Operand_Variable("changed");
$e2 = $factory->construct("equals", array($l2, $l2v));

$and = $factory->construct("and", array($e1, $e2));
$set = $factory->construct("set", array($and));

$l3 = new Expression_Operand_Variable("username");
$l3v = new Expression_Operand_Variable("johndoe");
$e3 = $factory->construct("equals", array($l3, $l3v));

$l4 = new Expression_Operand("status");
$l4v = new Expression_Operand_Variable("changed");
$e4 = $factory->construct("equals", array($l4, $l4v));
$not = $factory->construct("not", array($e4));

$and1 = $factory->construct("and", array($e3, $not));
$where = $factory->construct("where", array($and1));


$root = $factory->construct("update", array($table, $set, $where));
echo $root . "<br />";
?>